package com.hg.admin.service;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.hg.admin.db.dao.AdminDAO;
import com.hg.admin.db.dto.AdminDTO;
import com.hg.admin.vo.VO4admin;
import com.hg.util.common.Sec;

public class AdminService
{
	public AdminDAO adminDao = new AdminDAO();

	// 登录
	public JSONObject loginService(VO4admin admin)
	{	
		JSONObject json = new JSONObject();
		json.put("id", -1);
		AdminDTO dto = adminDao.findAdmin(admin.getUsername());
		if (dto != null) {
			String psw = Sec.MD5Encrypt(admin.getPassword(), 32);
			if (dto.getPassword().equals(psw)) {
				json.put("id", Integer.valueOf(dto.getId()));
				json.put("admin_role_id", dto.getAdmin_role_id());
			} else {
				json.put("id", -2);
			}
		}
		return json;

	}

	public List<VO4admin> findAllAdmin()
	{
		List<VO4admin> ls = null;
		AdminDAO dao = new AdminDAO();
		List<AdminDTO> list = dao.findAllAdmin();
		if (list != null)
		{
			ls = new ArrayList<VO4admin>();
			for (int i = 0; i < list.size(); i++)
			{
				AdminDTO dto = list.get(i);
				VO4admin vo = new VO4admin();
				vo.setId(dto.getId());
				vo.setPassword(dto.getPassword());
				vo.setUsername(dto.getUsername());
				ls.add(vo);
			}
		}
		return ls;
	}

	public int passwordUpdate(String id, String oldpass, String newpass1, String newpass2)
	{
		AdminDAO dao = new AdminDAO();
		if (newpass1.equals(newpass2))
		{
			if (dao.passwordUpdate(id, oldpass, newpass1))
			{
				return 0;
			}
			return 1;
		}
		else
		{
			return 2;
		}
	}
	
	public boolean checkAdmin(String id,String adm_name){
		return adminDao.checkAdmin(id, adm_name);
	}
	
	
	/****************************************管理员移动端**************************************/
	private VO4admin format(AdminDTO dto){
		VO4admin vo = new VO4admin();
		vo.setId(dto.getId());
		vo.setPassword(dto.getPassword());
		vo.setUsername(dto.getUsername());
		return vo;
	}
	public VO4admin getRowByUserId(String user_id){
		AdminDTO dto = adminDao.getRowByUserId(user_id);
		
		if(null!=dto){
			return this.format(dto);
		}else{
			return null;
		}
	}
}
